// pages/search/search.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // q: '',
    value: '',
    historyList: [
      { name: '士大夫', id: 1 },
      { name: 'dsasd', id: 2 },
      { name: '法撒旦大苏打', id: 3 },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const historyList = wx.getStorageSync('historyList')
    this.setData({
      historyList,
    })
  },
  //搜索
  getSearch() {
    wx.navigateTo({
      url: '/pages/search-list/list?q=' + this.data.value,
    })
    const newHistoryList = this.data.historyList
    newHistoryList.unshift({
      id: +new Date(),
      name: this.data.value,
    })
    this.setData({
      historyList: newHistoryList,
    })
    wx.setStorageSync('historyList', this.data.historyList)
  },
  //删除历史列表的一项
  delete(ev) {
    console.log(ev.mark.id)
    const historyList = this.data.historyList.filter((item) => item.id !== ev.mark.id)
    this.setData({ historyList })
  },
  //删除所有历史记录
  deleteAll() {
    this.setData({ historyList: [] })
    wx.removeStorageSync('historyList')
  },
  //根据历史记录搜索
  sreachThisHistory(ev) {
    console.log(ev.mark.q)
    wx.navigateTo({
      url: '/pages/search-list/list?q=' + ev.mark.q,
    })
  },
})
